<template>
  <div class="page-container">
    <div class="welcome-section">
      <h1>欢迎来到课程管理系统</h1>
      <p>{{ userStore.user.username }}，欢迎您！</p>
      
      <!-- 添加仓库链接 -->
      <div class="repo-link-container">
        <el-button 
          type="primary" 
          plain 
          size="large"
          @click="openRepository"
          class="repo-button"
        >
          <el-icon><Link /></el-icon>
          查看项目仓库
        </el-button>
      </div>
    </div>
   
    <div v-if="loading" class="loading-container">
      <el-skeleton :rows="5" animated />
    </div>
   
    <div v-else-if="error" class="error-container">
      <el-result
        icon="error"
        title="加载失败"
        :sub-title="error"
      >
        <template #extra>
          <el-button type="primary" @click="loadCategories">重新加载</el-button>
        </template>
      </el-result>
    </div>
   
    <div v-else class="categories-container">
      <div v-if="categories.length === 0" class="empty-container">
        <el-empty description="暂无课程分类">
          <el-button type="primary" @click="$router.push('/add-category')">
            添加第一个分类
          </el-button>
        </el-empty>
      </div>
     
      <div v-else>
        <CourseCategoryBlock
          v-for="category in displayCategories"
          :key="category.id"
          :category="category"
        />
       
        <div v-if="categories.length > 4" class="more-categories">
          <el-button type="text" size="large">
            查看更多分类...
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useUserStore } from '../stores/user'
import CourseCategoryBlock from '../components/CourseCategoryBlock.vue'
import * as api from '../services/api'
import { ElMessage } from 'element-plus'
import { Link } from '@element-plus/icons-vue'

const userStore = useUserStore()
const categories = ref([])
const loading = ref(false)
const error = ref('')

// 只显示前4个分类
const displayCategories = computed(() => {
  return categories.value.slice(0, 4)
})

// 打开仓库链接
const openRepository = () => {
  window.open('https://gitee.com/chen-qingyuan888/front-end?source=header_my_projects', '_blank')
}

const loadCategories = async () => {
  try {
    loading.value = true
    error.value = ''
   
    const response = await api.getAllCategories()
    categories.value = Array.isArray(response) ? response : []
   
  } catch (err) {
    console.error('获取课程分类失败:', err)
    error.value = '获取课程分类失败，请检查网络连接'
    ElMessage.error('获取课程分类失败')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadCategories()
})
</script>

<style scoped>
.page-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.welcome-section {
  text-align: center;
  margin-bottom: 40px;
  padding: 40px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
}

.welcome-section h1 {
  font-size: 32px;
  margin: 0 0 10px 0;
  font-weight: bold;
}

.welcome-section p {
  font-size: 18px;
  margin: 0 0 20px 0;
  opacity: 0.9;
}

.repo-link-container {
  margin-top: 20px;
}

.repo-button {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  font-size: 16px;
  padding: 12px 24px;
  transition: all 0.3s ease;
}

.repo-button:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
}

.loading-container {
  padding: 40px;
}

.error-container, .empty-container {
  padding: 40px;
  text-align: center;
}

.categories-container {
  margin-top: 20px;
}

.more-categories {
  text-align: center;
  margin-top: 30px;
  padding: 20px;
}

.more-categories .el-button {
  font-size: 16px;
}
</style>